﻿namespace EyeOpen.DddOrm.Data.Test
{
	using EyeOpen.DddOrm.Domain.Factories;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	using SharpTestsEx;

	[TestClass]
	public class ContentTest
	{
		private NhibernateMediaUnitOfWorkDataSessionFactory mediaUnitOfWorkDataSessionFactory;

		[TestInitialize]
		public void Initiliaze()
		{
			mediaUnitOfWorkDataSessionFactory = new NhibernateMediaUnitOfWorkDataSessionFactory();

			CreatePortalAndCategories();
		}

		[TestMethod]
		public void AddContentWithCategoriesToPortal()
		{
			using (var uow = mediaUnitOfWorkDataSessionFactory.CreateUnitOfWork())
			{
				var portal = uow.PortalRepository.ByName("Wine");

				var categoryList =
					uow
						.CategoryRepository
						.CreateIfNotPresent(".NET/C#", ".NET/VB.NET");

				portal.AddContent(ContentFactory.Create("Home", "Home description", categoryList));

				uow.PortalRepository.Update(portal);

				uow.Commit();
			}

			using (var uow = mediaUnitOfWorkDataSessionFactory.CreateUnitOfWork())
			{
				uow
					.PortalRepository
					.ByName("Wine")
					.ContentList
					.Should().Not.Be.Null();
			}
		}

		private void CreatePortalAndCategories()
		{
			using (var uow = mediaUnitOfWorkDataSessionFactory.CreateUnitOfWork())
			{
				var portal = PortalFactory.CreatePortalByName("Wine");

				uow.PortalRepository.Add(portal);

				uow.Commit();
			}

			using (var uow = mediaUnitOfWorkDataSessionFactory.CreateUnitOfWork())
			{
				uow.CategoryRepository.CreateIfNotPresent(".NET/C#", ".NET/VB.NET");

				uow.Commit();
			}
		}
	}
}